package ai;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.Map;

public class StatisticsManager {
	private Map<String, AggregateStatistics> statistics;

	public StatisticsManager() {
		statistics = new HashMap<String, AggregateStatistics>();
	}

	public void saveStatisticsOnFiles(String path, String name) {
		String nl = System.getProperty("line.separator");
		int i = 0;
		for (String s : statistics.keySet()) {
			AggregateStatistics as = statistics.get(s);
			BufferedWriter writer;
			try {
				writer = new BufferedWriter(new FileWriter(
						new File(path + name + "_" + i + ".txt")));
					
				writer.write(s+nl+nl);
				writer.write(as.toString());
				writer.flush();
				writer.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			i++;
		}

	}

	public void registerStatistic(GameStatistics gs) {
		String nl = System.getProperty("line.separator");
		String id = gs.getBoardRows() + "-" + gs.getBoardCols() + nl
				+ gs.getPlayer1Info() + nl + gs.getPlayer2Info();
		if (statistics.containsKey(id))
			statistics.get(id).registerGameStatistics(gs);
		else {
			AggregateStatistics as = new AggregateStatistics();
			as.registerGameStatistics(gs);
			statistics.put(id, as);
		}
	}
}
